package com.example.rentalclearing.controller;

import com.example.rentalclearing.common.ClearingResult;
import com.example.rentalclearing.service.ClearingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/clearing")
@Tag(name = "核销管理", description = "批量核销租金收款")
public class ClearingController {

    private final ClearingService clearingService;

    public ClearingController(ClearingService clearingService) {
        this.clearingService = clearingService;
    }

    @PostMapping("/start")
    @Operation(summary = "启动核销任务", description = "传入收款日期，如 2025-10-17")
    public ClearingResult startClearing(@RequestParam String date) {
        try {
            return clearingService.startClearing(date);
        } catch (Exception e) {
            log.error("核销任务启动失败", e);
            throw new RuntimeException("核销任务失败");
        }
    }
}